package com.code.petapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import androidx.core.app.ActivityCompat;

import com.bumptech.glide.Glide;
import com.code.petapp.bean.LostBean;
import com.code.petapp.databinding.ActivityLostBinding;
import com.code.petapp.http.HttpListener;
import com.code.petapp.util.LostUtils;
import com.code.petapp.util.MediaUtils;
import com.code.petapp.util.SPCommonUtils;
import com.code.petapp.util.UploadUtils;
import com.code.petapp.util.VersionUtils;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.interfaces.OnResultCallbackListener;

import java.io.File;
import java.util.ArrayList;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class LostActivity extends BaseActivity implements OnResultCallbackListener<LocalMedia> {
    ActivityLostBinding binding;
    public static final String TAG = "LostActivity";
    boolean LOAD;
    String name;
    String desc;
    int id;
    String imgUrl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityLostBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        Intent intent = getIntent();
        LOAD = intent.getBooleanExtra("LOAD", false);
        name = intent.getStringExtra("name");
        desc = intent.getStringExtra("desc");
        id = intent.getIntExtra("id", 0);
        if (LOAD) {
            load();
            binding.submit.setOnClickListener(view -> {
                binding.submit.setEnabled(false);
                update();
            });
        } else {
            binding.submit.setOnClickListener(view -> {
                binding.submit.setEnabled(false);
                save();
            });
        }
        setImg();

    }

    private void load() {
        binding.name.setText(name);
        binding.desc.setText(desc);
    }

    private void save() {
        String name = binding.name.getText().toString();
        String desc = binding.desc.getText().toString();
        if (name.isEmpty() || desc.isEmpty()) {
            toastShortShow("请填写完整信息");
        } else {
            LostBean lostBean = new LostBean();
            lostBean.setName(name);
            lostBean.setDescr(desc);
            lostBean.setImg(imgUrl);
            LostUtils.add(SPCommonUtils.getInstance().getToken(), lostBean, new HttpListener<>() {
                @Override
                public void success(String object) {
                    toastShortShow("上传成功");
                    finish();
                }

                @Override
                public void error(int code, String error) {
                    toastShortShow(error);
                }
            });
        }
    }

    private void update() {
        LostBean lostBean = new LostBean();
        lostBean.setName(binding.name.getText().toString());
        lostBean.setDescr(binding.desc.getText().toString());
        lostBean.setImg(imgUrl);
        lostBean.setId(id);
        LostUtils.update(SPCommonUtils.getInstance().getToken(), lostBean, new HttpListener<>() {
            @Override
            public void success(String object) {
                toastShortShow("修改成功");
                finish();
            }

            @Override
            public void error(int code, String error) {
                toastShortShow(error);
            }
        });
    }

    private void setImg() {
        binding.defaultImg.setOnClickListener(view ->  {binding.defaultImg.setEnabled(false);
        mediaSale();});
    }

    /**
     * 图片视频选择
     */
    private void mediaSale() {
        boolean isAllGranted = VersionUtils.Companion.checkPermissionAllGranted(VersionUtils.Companion.getPermission(), this);
        Log.d("ZZ", "isAllGranted:" + isAllGranted);
        if (isAllGranted) {
            MediaUtils.getPhoto(this, 1, this);
        } else {
            ActivityCompat.requestPermissions(this, VersionUtils.Companion.getPermission(), 10000);
        }
    }

    @Override
    public void onResult(ArrayList<LocalMedia> mediaVideo) {
        if (!mediaVideo.isEmpty()) {
            for (int i = 0; i < mediaVideo.size(); i++) {
                LocalMedia localMedia = mediaVideo.get(i);
                String realFilePath;
                if (localMedia.isCompressed()) {
                    realFilePath = localMedia.getCompressPath();
                } else if (localMedia.isCut()) {
                    realFilePath = localMedia.getCutPath();
                } else {
                    realFilePath = localMedia.getRealPath();
                }
                Log.d(TAG, "realFilePath:" + realFilePath);
                if (!TextUtils.isEmpty(realFilePath)) {
                    File file = new File(realFilePath);
                    RequestBody requestFile = RequestBody.create(MediaType.parse("image/*"), file);
                    MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), requestFile);
                    UploadUtils.upload(SPCommonUtils.getInstance().getToken(), body, new HttpListener<>() {
                        @Override
                        public void success(String data) {
                            imgUrl = data;
                            Log.d("src is:", data);
                            Glide.with(LostActivity.this)
                                    .load(data)
                                    .into(binding.defaultImg);
                        }

                        @Override
                        public void error(int code, String error) {

                        }
                    });
                }
            }
        }
    }

    @Override
    public void onCancel() {

    }
}